<?php
/**
 * Excursion plugin class
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @version	1.0
 * @link
 * @since  	08-2009
 */
class RM_Plugin_Excursion extends RM_Plugin implements RM_Search_Advanced_Interface{
    public function  __construct()
    {
        $this->name = 'Excursion';
    }

    public function disable()
    {
        throw new RM_Exception(RM_Environment::getInstance()->getTranslation()->_('Admin.Excursion.Main', 'DisableAlert'));
    }

    /**
     * This method will return node object for main admin menu tree.
     * Every child classes could overload this method to return any of the node object.
     * If there is no need to present a plugin in the main admin tree overloaded method should return NULL
     *
     * @return stdClass | null
     */
    public function getNode()
    {
        $std = new stdClass;
        $std->id = $this->name.'_ListJson';
        $std->text = $this->getName();
        $std->leaf = 'true';
        $std->iconCls = 'RM_plugins_leaf_icon';
        return $std;
    }

    /**
     * This method will return node object for configuration admin menu tree.
     * Every child classes could overload this method to return any of the node object.
     * If there is no need to present a plugin in the configuration admin tree overloaded method should return NULL
     *
     * @return stdClass | null
     */
    public function getConfigNode()
    {
        return null;
    }

    /**
     * Returns all unit ids that are passed criteria Excursion
     *
     * @param RM_Unit_Search_Criteria $criteria
     * @return array, false
     */
    function getAdvancedSearchUnitIDs(RM_Unit_Search_Criteria $criteria){

        $model = new RM_ExcursionType();

        if (RM_Environment::getConnector()->getModule() == 'admin' && !$criteria->showExcursionUnits){
            return false;
        }

        if ($criteria->includeExcursionUnits){
            return false;
        }

        if ($criteria->showExcursionUnits){
            // return the opion units only
            $units = $model->getAllExcursionUnits();
        } else {
            // return the id's of non-Excursion units
            $units = $model->getAllNonExcursionUnits()->toArray();
        }

        $returnArray = array();
        foreach($units as $unit){
            $returnArray[] = $unit['id'];
        }
        return $returnArray;

    }

    public function upgrade(){
        // userdata
        $pluginViewFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'plugins',
            $this->name,
            'views',
            'user',
            'scripts',
            'details'
        ));


        $mainFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details'

        ));

        $fileSource1 = implode(DIRECTORY_SEPARATOR, array($pluginViewFolderPath.DIRECTORY_SEPARATOR,'excursion_addtocart_details.phtml'));
        $fileDestin1 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR,'excursion_addtocart_details.phtml'));
        $renameStatus1 = rename($fileSource1, $fileDestin1);

        $fileSource2 = implode(DIRECTORY_SEPARATOR, array($pluginViewFolderPath.DIRECTORY_SEPARATOR,'excursion_associatedunits_details.phtml'));
        $fileDestin2 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR,'excursion_associatedunits_details.phtml'));
        $renameStatus2 = rename($fileSource2, $fileDestin2);
    }

    public function install(){
        parent::install();

        $model = new RM_UnitTypeConfig();
        $model->register(8, array(7));

        // userdata
        $pluginViewFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'plugins',
            $this->name,
            'views',
            'user',
            'scripts',
            'details'
        ));


        $mainFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details'

        ));

        $fileSource1 = implode(DIRECTORY_SEPARATOR, array($pluginViewFolderPath.DIRECTORY_SEPARATOR,'excursion_addtocart_details.phtml'));
        $fileDestin1 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR,'excursion_addtocart_details.phtml'));
        $renameStatus1 = rename($fileSource1, $fileDestin1);

        $fileSource2 = implode(DIRECTORY_SEPARATOR, array($pluginViewFolderPath.DIRECTORY_SEPARATOR,'excursion_associatedunits_details.phtml'));
        $fileDestin2 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR,'excursion_associatedunits_details.phtml'));
        $renameStatus2 = rename($fileSource2, $fileDestin2);

        RM_Filesystem::deleteFolder($pluginViewFolderPath);

        if ($renameStatus1 && $renameStatus2) return true;
        return false;
    }

    public function uninstall() {
        parent::uninstall();

        $model = new RM_UnitTypes();
        $type = $model->findByName('Excursion');

        $unitModel = new RM_Units();
        $unitModel->update(
            array(
                'type_id' => RM_UnitTypes::DEFAULT_TYPE,
                'published' => 0
            ),
            'type_id='.$type->id
        );

        $file1 = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details',
            'excursion_addtocart_details.phtml'
        ));
        RM_Filesystem::deleteFile($file1);

        $file2 = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details',
            'excursion_associatedunits_details.phtml'
        ));
        RM_Filesystem::deleteFile($file2);
    }
}
